package jui.view
{
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.utils.setTimeout;
	
	import jui.maps.events.JUIEvent;
	import jui.view.extended.JUITextArea;
	
	import mx.containers.VBox;
	import mx.controls.TextInput;
	
	public class MessageWindowBase extends VBox
	{
		public var messageInput: MessageInput;
		
		public var messageOutput: JUITextArea;
		
		public var jDeleteMessageId: TextInput;
		
		private function getOutputTextMetrics(): void
		{
			var lastCharIndex: int = messageOutput.text.length - 1;//
			var cb: Rectangle = messageOutput.getCharBounds( lastCharIndex );
			
			trace("Rectified: " + cb );
		}
		
		/**
		 * Smart autoscroll - scrolls only if message window is scrolled to the bottom already
		 */ 
		protected function autoScrollOutput():void
		{
			//setTimeout(getOutputTextMetrics, 1000/24);
			//callLater(getOutputTextMetrics);
			/*try{
			var m: TextLineMetrics = messageOutput.getLineMetrics(10);
			}
			catch (e: Error)
			{
			trace("Line n/a");
			}*/
			
			trace("max " + messageOutput.maxVerticalScrollPosition + " - cur " + messageOutput.verticalScrollPosition + 
				" = " + ( messageOutput.maxVerticalScrollPosition - messageOutput.verticalScrollPosition ) );
			if( messageOutput.maxVerticalScrollPosition - messageOutput.verticalScrollPosition < 15 )
			{
				messageOutput.verticalScrollPosition = messageOutput.maxVerticalScrollPosition;
			}
		}
		
		protected function messageOutput_clickHandler(e:MouseEvent):void
		{
			var cindex:int = messageOutput.getCharIndexAtPoint(e.localX, e.localY);
			
			//trace("Char index = " + cindex); 
			if (cindex != -1) 
			{
				var frame:Rectangle = messageOutput.getCharBounds(cindex);
				
				//trace("Frame = " + frame );
				
				//spotlight.graphics.clear();    
				//spotlight.graphics.beginFill(0xFFFF00, .35);
				//spotlight.graphics.drawRect((frame.x + 10), (frame.y + 10), frame.width, frame.height);            
				//spotlight.graphics.endFill();
			}
		} 			
		
		private function sendSpecialMessage(jid:String, messageText: String):void 
		{
			var juiEvent: JUIEvent = new JUIEvent(JUIEvent.JUI_SEND_MESSAGE);
			juiEvent.toJID = jid;
			juiEvent.text = messageText;
			
			dispatchEvent(juiEvent);
		}
		
		protected function jLogin():void
		{
			sendSpecialMessage( messageInput.getTo(), "LOGIN" );
		}
		
		protected function jDeleteLastMessage():void
		{
			sendSpecialMessage( messageInput.getTo(), "D LAST" );
		}
		
		protected function jDeleteMessageById():void
		{
			var messageId: String = jDeleteMessageId.text;
			if (messageId.indexOf("#") != 0)
			{
				messageId = "#" + messageId;	
			}
			
			sendSpecialMessage( messageInput.getTo(), "D " + messageId );
		}
		
		protected function autoScroll_clickHandler(event:MouseEvent):void
		{
			var jEvent: JUIEvent = new JUIEvent(JUIEvent.JUI_SETTINGS_AUTOSCROLL)
			jEvent.flag = true;	
			dispatchEvent(jEvent);
		}
		
		public function MessageWindowBase()
		{
			super();
		}
	}
}